package com.zt.dev.standardManage.controller;

import com.zt.dev.commons.ServerResponse;
import com.zt.dev.standardManage.entity.vo.Model;
import com.zt.dev.standardManage.entity.vo.Standards;
import com.zt.dev.standardManage.service.ModelsService;
import com.zt.dev.standardManage.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/modelManage")
public class ModelsController {

    @Autowired
    private ModelsService modelsService;

    /**
     * 新增评估模板
     * @param model
     * @return
     */
    @RequestMapping("/insertModel")
    @ResponseBody
    public ServerResponse insertModel(Model model){
        if(model != null){
            int result = modelsService.insertModels(model);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("添加成功");
            }else{
                return ServerResponse.createByErrorMessage("添加失败");
            }
        }
        return ServerResponse.createByErrorMessage("参数为空");
    }

    /**
     * 分页查询模板列表
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/getAllModel")
    @ResponseBody
    public ServerResponse getAllModel(@RequestParam(value = "currentPage", defaultValue = "1", required = true)int currentPage, int pageSize,String modelName){
        PageBean<Model> modelList = modelsService.getAllModels(currentPage, pageSize,modelName);
        if(modelList != null){
            return ServerResponse.createBySuccess(modelList);
        }
        return ServerResponse.createByErrorMessage("查询为空");
    }

    /**
     * 批量删除评估模板
     * @param ids
     * @return
     */
    @RequestMapping("/deleteModels")
    @ResponseBody
    public ServerResponse deleteModels(Integer[] ids){
        if(ids.length > 0){
            int result = modelsService.deleteModels(ids);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("删除成功");
            }else{
                return ServerResponse.createByErrorMessage("删除失败");
            }
        }
        return ServerResponse.createByErrorMessage("参数为空");
    }

    /**
     * 修改模板
     * @param model
     * @return
     */
    @RequestMapping("/updateModel")
    @ResponseBody
    public ServerResponse updateModel(Model model){
        if(model != null){
            int result = modelsService.updateModel(model);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("修改成功");
            }else{
                return ServerResponse.createByErrorMessage("修改失败");
            }
        }
        return ServerResponse.createByErrorMessage("参数为空");
    }

    /**
     * 依据模板，生成准则
     * @param standards,id
     * @return
     */
    @RequestMapping("/addStandard")
    @ResponseBody
    public ServerResponse addStandard(Standards standards,Integer modelId){
        if(standards != null){
            int result = modelsService.addStandard(standards,modelId);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("生成成功");
            }else{
                return ServerResponse.createByErrorMessage("生成失败");
            }
        }
        return ServerResponse.createByErrorMessage("参数为空");
    }

    /**
     * 统计当前月各类别量
     * @return
     */
    @RequestMapping("/getModelByMon")
    @ResponseBody
    public ServerResponse getModelByMon(String createTime){
        Map<String,Object> resultMap = modelsService.getModelByMon(createTime);
        if(resultMap != null){
            return ServerResponse.createBySuccess(resultMap);
        }
        return ServerResponse.createByErrorMessage("查询为空");
    }
}

